package com.lg.erp.controller.zhanghuan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.zhanghuan.TDriver;
import com.lg.erp.entity.zhanghuan.TLogistics;
import com.lg.erp.entity.zhanghuan.TVehicle;
import com.lg.erp.service.zhanghuan.TDriverService;
import com.lg.erp.service.zhanghuan.TLogisticsService;
import com.lg.erp.service.zhanghuan.TVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/***
 *
 * 驾驶员信息
 * */
@RestController
public class TDriverController {
    @Autowired
    private TDriverService tDriverService;

    @Autowired
    private TVehicleService tVehicleService;


    @GetMapping("/drivers")
    public JsonResult pageTDriver(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,String driverName, String driverLicense) {
        Page<TDriver> page = new Page<TDriver>(current, 5);
        IPage<TDriver> tDriverIPage = tDriverService.pageTDriver(page, driverName, driverLicense);
        return new JsonResult(1, "ok", tDriverIPage);
    }

    //查询车牌号
    @GetMapping("/vehiclesNames")
    public JsonResult AllDrivers() {
        List<TVehicle> list = tVehicleService.list();
        return new JsonResult(1, "ok", list);
    }

    @PostMapping("/driver")
    public JsonResult addDriver(@RequestBody TDriver tDriver) {
        String id = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
        LocalDateTime dateTime = LocalDateTime.now();
        tDriver.setDriverId(id);
        tDriver.setCreateTime(dateTime);
        tDriver.setStatus("有任务");
        tDriverService.save(tDriver);
        return new JsonResult(1, "ok", null);
    }

    @PutMapping("/driver")
    public JsonResult updateDriver(@RequestBody TDriver tDriver) {
        LocalDateTime dateTime = LocalDateTime.now();
        tDriver.setUpdateTime(dateTime);
        QueryWrapper<TDriver> wrapper = new QueryWrapper<>();
        wrapper.eq("driver_id", tDriver.getDriverId());
        tDriverService.update(tDriver, wrapper);
        return new JsonResult(1, "ok", null);
    }
    @DeleteMapping("/driver/{id}")
    public JsonResult deleteDriver(@PathVariable String id) {
        tDriverService.remove(new QueryWrapper<TDriver>().eq("driver_id", id));
        return new JsonResult(1, "ok", null);

    }





}
